package com.lwz.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwz.eduservice.entity.EduTeacher;
import com.lwz.eduservice.entity.vo.TeacherQuery;
import com.lwz.eduservice.mapper.EduTeacherMapper;
import com.lwz.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.querydsl.QuerydslUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author Lw中
 * @since 2020-11-03
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Override
    public boolean removeById(Serializable id) {
        Integer result = baseMapper.deleteById(id);
        return null != result && result > 0;
    }

    /**
     * LWZ TODO : 2020/11/19 使用map返回数据的分页业务逻辑层
     */
    @Override
    public Map<String, Object> getTeacherFrontList(Page<EduTeacher> teacherPage) {
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        // 将数据通过id进行排序
        wrapper.orderByDesc("id");
        // 将分页数据封装到teacherPage中
        baseMapper.selectPage(teacherPage, wrapper);

        // 将teacherPage中的数据取出来，用于前端分页使用
        List<EduTeacher> records = teacherPage.getRecords();
        long current = teacherPage.getCurrent();
        long size = teacherPage.getSize();
        long total = teacherPage.getTotal();
        long pages = teacherPage.getPages();
        boolean hasNext = teacherPage.hasNext();
        boolean hasPrevious = teacherPage.hasPrevious();

        // 将teacherPage中数据封装到map中
        Map<String, Object> map = new HashMap<>();
        map.put("items",records);
        map.put("current", current);
        map.put("size", size);
        map.put("total", total);
        map.put("pages", pages);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        // 将map集合返回
        return map;
    }
}
